//
//  AllOrderDetailViewController.m
//  Restaurant
//
//  Created by wanghuan on 15/5/5.
//  Copyright (c) 2015年 wanghuan. All rights reserved.
//

#import "AllOrderDetailViewController.h"

@interface AllOrderDetailViewController () {

    UIButton *btn;

}

@end

@implementation AllOrderDetailViewController

- (void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:YES];
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.view.backgroundColor = AppBackGroundColor;
    
    _dataArr = [[NSMutableArray alloc]init];
    
    _detailTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, APPWith, APPHeight-64-49) style:UITableViewStyleGrouped];
    _detailTableView.delegate = self;
    _detailTableView.dataSource = self;
    
    _detailTableView.backgroundColor = AppBackGroundColor;
    _detailTableView.separatorColor = _detailTableView.backgroundColor;
    
    [self.view addSubview:_detailTableView];
    
    _requestManager = [[WHAFHTTPRequest alloc]init];
    
    _requestManager.Sblock = ^(id obj) {
    
        [self downLoad:obj];
    
    };
    
    _requestManager.Fblock = ^(NSString *error) {
    
        UIAlertView *alterView = [[UIAlertView alloc]initWithTitle:@"提示" message:NSLocalizedString(@"网络连接失败，请稍后再试", nil) delegate:nil cancelButtonTitle:NSLocalizedString(@"确定", nil) otherButtonTitles:nil, nil];
        
        [alterView show];
    
    };

    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    
    [_requestManager createWHAFHttpRequestUrl:[NSString stringWithFormat:AllOrderDetailUrl,[userDefault objectForKey:usersId],self.allOrderModel.orderId]];

    
}

- (void)downLoad:(id)data {

    if (![[ data objectForKey:@"success"] boolValue]) {
        
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, APPHeight/2-15, APPWith, 30)];
        label.text = [data objectForKey:@"msg"];
        
        label.textAlignment = NSTextAlignmentCenter;
        
        [_detailTableView addSubview:label];
        
        return;
    }
    
    NSArray *objArr = [[data objectForKey:@"obj"] objectForKey:@"products"];
    
    for (NSDictionary *dic in objArr) {
    
        shoppingModel *model = [[shoppingModel alloc]init];
        model.nameStr = [NSString stringWithFormat:@"%@",[dic objectForKey:@"productName"]];
        model.prices = [NSString stringWithFormat:@"￥%@",[dic objectForKey:@"perprice"]];
        model.number = [NSString stringWithFormat:@"X%@",[dic objectForKey:@"num"]];
        
        [_dataArr addObject:model];
        
    }
    
    //给发表评论添加一个占位符
    shoppingModel *model = [[shoppingModel alloc]init];
    
    [_dataArr addObject:model];
    
    [_detailTableView reloadData];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    
    
    return 1;
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return [_dataArr count];

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section == _dataArr.count-1) {
        return 80;
    }

    return 40;
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return 2;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 2;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == [_dataArr count]-1) {
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"button"];
        
        if (!cell) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"button"];
        }
        
        cell.backgroundColor = [UIColor clearColor];
        
        btn = [[UIButton alloc]initWithFrame:CGRectMake(40, 40, APPWith-80, 40)];
        
        [btn setBackgroundImage:[UIImage imageNamed:@"botton2.png"] forState:UIControlStateNormal];
        
        [btn setTitle:@"发表评论" forState:UIControlStateNormal];
        
        [btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
        
        [cell addSubview:btn];
        
        return cell;

    }
    
    OrderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"order"];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    if (!cell) {
        cell = [[NSBundle mainBundle]loadNibNamed:@"OrderTableViewCell" owner:self options:nil].lastObject;
    }
    
    shoppingModel *model = [_dataArr objectAtIndex:indexPath.section];
    
    
    cell.nameLabel.text = model.nameStr;
    cell.numberLabel.text = model.number;
    cell.priceLabel.text =  model.prices;
    
    return cell;
    
    
        
    
}


#pragma mark - 发表评论按钮

- (void)btnClick {

    ReviewViewController *rvc = [[ReviewViewController alloc]init];
    
    [self.navigationController pushViewController:rvc animated:YES];

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)backClick:(UIButton *)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
}
@end
